<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>短网址生成</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctx.contextPath}/layui/css/layui.css">
    <style>
        .header {
            text-align: center;
            font-size: 30px;
            margin-top: 100px;
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
<div class="layui-container">
    <div class="header">
        <i class="layui-icon" style="font-size: 30px; color: #1E9FFF;">&#xe609;</i>
        <span>短网址生成</span>
    </div>
    <form class="layui-form">
        <div class="layui-form-item">
                 <textarea id="url_field" name="url" lay-verify="required" placeholder="请输入将要缩短的长网址"
                           class="layui-textarea"></textarea>
        </div>
        <div class="layui-form-item  layui-col-md6">
            <div>
                <button class="layui-btn" lay-submit lay-filter="formUrl">生成</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="${ctx.contextPath}/layui/layui.js"></script>
<script>
    layui.use(['layer', 'form'], function () {
        var form = layui.form
            , $ = layui.$
            , layer = layui.layer;

        //监听提交
        form.on('submit(formUrl)', function (data) {
            var long_url = data.field.url;
            if (long_url.length < 10) {
                layer.msg('输入长一点的网址吧!');
                return false;
            }
            $.post("shorten", {accessCode: 'shorturl_code', url: long_url}, function (result) {
                if (result.code === 200) {
                    $("#url_field").val(result.url);
                } else {
                    layer.msg(result.msg);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>